package com.hnx.admin.system.servce.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hnx.admin.api.http.HttpHeaderAPI;
import com.hnx.admin.api.security.api.VerifySystem;
import com.hnx.admin.api.security.core.SecurityPropertyAPI;
import com.hnx.admin.common.ResultJson;
import com.hnx.admin.common.redis.core.RedisCore;
import com.hnx.admin.common.security.utils.JSONUtil;
import com.hnx.admin.common.utils.IpUtil;
import com.hnx.admin.system.core.SysConfigProperties;
import com.hnx.admin.system.servce.SysConfigService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


/*
创建人： 倔强的头发
创建时间：2024/7/14
功能描述:

*/
@Service
@RequiredArgsConstructor
public class VerifySystemImpl implements VerifySystem, HttpHeaderAPI, SecurityPropertyAPI {
    private final RedisCore redisCore;
    private final SysConfigService sysConfigService;
    private final SysConfigProperties configProperties;
    private final JSONUtil jsonUtil;
    @Override
    public boolean verify(HttpServletRequest request,HttpServletResponse response) {
        String blackStr = sysConfigService.selectConfigByKey(configProperties.getBlackIPList());
        if(StrUtil.isNotBlank(blackStr) && !"0".equals(blackStr)){
            if(ObjectUtil.isNotEmpty(request)) {
                String ipAddr = IpUtil.getIpAddr(request);
                if(StrUtil.isNotBlank(ipAddr)){
                    boolean matchedIp = IpUtil.isMatchedIp(blackStr, ipAddr);
                    if(matchedIp){
                        jsonUtil.writeJSON(response, ResultJson.fail(NO_IP));
                    }
                    return !matchedIp;
                }
            }
        }
        return true;
        }
}
